package com.bishe.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bishe.entity.Association;
import com.bishe.entity.response.R;
import com.bishe.service.AssociationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("association")
@Api(tags = "关联")
public class AssociationController {

    @Resource
    private AssociationService associationService;

    @ApiOperation("线索关联")
    @PostMapping("addOrUpData")
    public R addOrUpData(@RequestBody List<Association> list) {
        if (CollUtil.isNotEmpty(list) && StrUtil.isEmpty(list.get(0).getId())) {
            String cid = list.get(0).getCid();
            associationService.remove(Wrappers.lambdaQuery(Association.class).eq(Association::getCid, cid));
            return R.ok(associationService.saveBatch(list));
        }

        if (CollUtil.isNotEmpty(list) && StrUtil.isNotEmpty(list.get(0).getId())) {
            return R.ok(associationService.updateBatchById(list));
        }
        return R.ok();
    }
}
